
//
//  vistorView.swift
//  BW新浪微博
//
//  Created by 静静静 on 15/9/2.
//  Copyright © 2015年 BossKai. All rights reserved.
//

import UIKit

protocol vistorViewLoginViewDelegate : NSObjectProtocol{
    func visitorLoginViewWillRegister();
    func visitorLoginViewWillLogin();
}
class vistorView: UIView {

    weak var delegate :vistorViewLoginViewDelegate?
    
    @objc private func clickRegister(){
        delegate?.visitorLoginViewWillRegister()
    }
    
    @objc private func clickLogin(){
        delegate?.visitorLoginViewWillLogin()
    }
    
    override init (frame:CGRect){
        super.init(frame: frame)
        setupUI()
        animateTopView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupUI()
    }
    
  
    func setupInfo(imageName:String,title:String){
        if imageName == "visitordiscover_feed_image_house"{
            topView.hidden = false
            coverView.hidden = false
        }
        else{
            topView.hidden = true
            coverView.hidden = true
        }
        iconView.image = UIImage(named: imageName)
        describLabel.text = title
    }
    // MARK: - 代码布局
    private func setupUI(){
        addSubview(iconView)
        addSubview(topView)
        addSubview(describLabel)
        addSubview(leftBtn)
        addSubview(rightBtn)
        addSubview(coverView)
        
//        iconView.translatesAutoresizingMaskIntoConstraints = false
//        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -10))
        
        topView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: topView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
//        // 提示：如果要设置一个固定数值，参照的属性，需要设置为 NSLayoutAttribute.NotAnAttribute，参照对象是 nil
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 224))
        
        describLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: describLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: describLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: topView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: describLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 244))
        
        
        leftBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: leftBtn, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: describLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 110))
        addConstraint(NSLayoutConstraint(item: leftBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        rightBtn.translatesAutoresizingMaskIntoConstraints = false
        addConstraint( NSLayoutConstraint(item: rightBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: describLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: describLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 5))
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 110))
        addConstraint(NSLayoutConstraint(item: rightBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        coverView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1
            , constant: 0))
    }
    // MARK: - 旋转动画
    private func animateTopView(){
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        anim.duration = 5
        anim.repeatCount = MAXFLOAT
        //设置动画不删除，当iconView 被销毁时候，动画会自动被释放
        anim.removedOnCompletion = false
        
        topView.layer.addAnimation(anim, forKey: "transform.rotation")
    }
    //MARK: - 懒加载控件
    
    private lazy var iconView : UIImageView = UIImageView()
    private lazy var topView : UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var describLabel : UILabel = {
        var label = UILabel()
        label.numberOfLines = 0
        label.textColor = UIColor.darkGrayColor()
        //label.text = self.descirbTitle
        label.textAlignment = NSTextAlignment.Center
        return label
    }()
     lazy var leftBtn : UIButton = {
       
        var btn = UIButton()
        
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickRegister", forControlEvents: UIControlEvents.TouchUpInside)

        return btn
        
    }()
     lazy var rightBtn : UIButton = {
        
        var btn = UIButton()
        
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.addTarget(self, action: "clickLogin", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
        }()

    private lazy var coverView : UIImageView = UIImageView (image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
   
    
}
